﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using BLL;

namespace QLNhanKhau
{
    public partial class UCTimKiem : UserControl
    {
        public UCTimKiem()
        {
            InitializeComponent();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtCMND.Text != "" && rdtCMND.Checked == true)
            {
                NhanKhauPublic nkcm = NhanKhauBLL.LoadNhanKhauByCMND(txtCMND.Text);
                if (nkcm == null)
                {
                    MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCMND.ResetText();
                    txtCMND.Focus();
                    UCTimKiem_Load(sender, e);
                    return;
                }
                txtHoTen.Text = nkcm.HoTen;
                if (nkcm.GioiTinh == true)
                    sbtGioiTinh.Value = true;
                else
                    sbtGioiTinh.Value = false;
                txtNoiSinh.Text = TinhBLL.LoadTinhByMaT(nkcm.NoiSinh);
                //if (nkcm.MaKT != 0)
                //    cboxTinhTrang.Checked = true;
                //else
                //    cboxTinhTrang.Checked = false;
                txtTrinhDo.Text = TrinhDoBLL.GetTrinhDoByMaTD(nkcm.MaTD).TenTrinhDo;
                txtDanToc.Text = DanTocBLL.LoadDanTocByMaDT(nkcm.MaDT).TenDanToc;
                txtQuocTich.Text = QuocTichBLL.LoadQuocTichByMaQT(nkcm.MaQT).TenQuocTich;
                txtTonGiao.Text = TonGiaoBLL.LoadTonGiaoByMaTG(nkcm.MaTG).TenTonGiao;
                txtDiaChi.Text = SoHoKhauBLL.LoadDiaChiSHKByMaNK(nkcm.MaNK);
                txtCMNDXem.Text = nkcm.CMND;

                if (nkcm.MaKS != 0)
                {
                    KhaiSinhPublic ks = KhaiSinhBLL.LoadKhaiSinhByMaKS(nkcm.MaKS);
                    txtMaKS.Text = nkcm.MaKS.ToString();
                    txtSoKS.Text = ks.SoKhaiSinh.ToString();
                    txtHoTenCha.Text = NhanKhauBLL.LoadNhanKhauByMaNK(ks.MaNKC).HoTen;
                    txtHoTenMe.Text = NhanKhauBLL.LoadNhanKhauByMaNK(ks.MaNKM).HoTen;
                    txtQuanHeChuHo.Text = QuanHeBLL.LoadQuanHeByMaQH(nkcm.MaQH).TenQuanHe;
                    txtQuanHeKS.Text = QuanHeBLL.LoadQuanHeByMaQH(ks.MaQH).TenQuanHe;
                    txtCanBoHoTichKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.CanBoHoTich).HoTen;
                    txtNguoiDangKiKS.Text = ks.NguoiDangki;
                    txtNguoiCapKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.NguoiCap).HoTen;
                    dateNgayCapKS.Value = (DateTime)ks.NgayCap;
                    cmbCanBoDangKiKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.CanBoDangKi).HoTen;
                    cmbNguoiNhanKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.NguoiNhan).HoTen;
                    txtChucVuNguoiCapKS.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND(ks.NguoiNhan).MaCV).TenChucVu;
                    dateNgayNhapKS.Value = (DateTime)ks.NgayNhap;
                }
                if (nkcm.MaKT != 0)
                {
                    cboxTinhTrang.Checked = true;
                    KhaiTuPublic kt = KhaiTuBLL.LoadKhaiTuByMaKT(nkcm.MaKT);

                    txtSoBaoTu.Text = kt.SoBaoTu.ToString();
                    txtSoChungTu.Text = kt.SoChungTu.ToString();
                    txtNoiCapBaoTu.Text = kt.NoiCapBaoTu;
                    txtNoiChet.Text = kt.NoiChet;
                    txtNoiOCuoiCung.Text = kt.NoiOCuoiCung;
                    txtNoiCapChungTu.Text = ThietLapBLL.LoadDiaChiByMaDiaChi(kt.NoiCap);
                    txtThoiGianChet.Text = kt.NgayChet.ToString();
                    txtNguoiCapKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.NguoiCap).HoTen;
                    txtNguoiKhaiTu.Text = kt.NguoiDangKi;
                    cmbNgayCapBaoTu.Value = kt.NgayCapBaoTu.Value;
                    txtCanBoHoTichKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.CanBoHoTich).HoTen;
                    cmbCanBoDangKiKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.CanBoDangKi).HoTen;
                    txtChucVuKT.Text = ChucVuBLL.LoadChucVuByMaCV(kt.ChucVu).TenChucVu;
                    txtChucVuNguoiNhanKT.Text = ChucVuBLL.LoadChucVuByMaCV(kt.ChucVuNguoiNhan).TenChucVu;
                    dateNgayCapKT.Value = (DateTime)kt.NgayCap;
                    dateNgayNhapKT.Value = (DateTime)kt.NgayNhap;
                    cmbNguoiNhanKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.NguoiNhan).HoTen;
                    txtLyDoChet.Text = kt.LyDoChet;

                }
                else
                    cboxTinhTrang.Checked = false;
                List<ChuyenKhauPublic> list_ck = ChuyenKhauBLL.LoadChuyenKhauByMaNK(nkcm.MaNK);
                if (list_ck.Count != 0)
                {
                    dgvChuyenKhau.DataSource = list_ck;
                    //dgvChuyenKhau.Columns[0].HeaderText = "Mã chuyển khẩu";
                    //dgvChuyenKhau.Columns[1].HeaderText = "Mã nhân khẩu";
                    dgvChuyenKhau.Columns[2].HeaderText = "Số";
                    dgvChuyenKhau.Columns[3].HeaderText = "Nơi chuyển";
                    dgvChuyenKhau.Columns[4].HeaderText = "Lý do chuyển";
                    dgvChuyenKhau.Columns[5].HeaderText = "Ngày cấp";
                    //dgvChuyenKhau.Columns[6].HeaderText = "Cán bộ nơi chuyển";
                    dgvChuyenKhau.Columns[7].HeaderText = "Người cấp";
                    //dgvChuyenKhau.Columns[8].HeaderText = "Chức vụ người chuyển";
                    dgvChuyenKhau.Columns[9].HeaderText = "Nơi cấp";
                    //dgvChuyenKhau.Columns[10].HeaderText = "Cán bộ nơi nhận";
                    //dgvChuyenKhau.Columns[11].HeaderText = "Ngày nhận";
                    dgvChuyenKhau.Columns[12].HeaderText = "Người nhận";
                    //dgvChuyenKhau.Columns[13].HeaderText = "Chức vụ người nhận";
                    //dgvChuyenKhau.Columns[14].HeaderText = "Nơi ở trước";
                    dgvChuyenKhau.Columns[15].HeaderText = "Chủ hộ";
                    dgvChuyenKhau.Columns[16].HeaderText = "Quan hệ";
                    dgvChuyenKhau.Columns[0].Visible = false;
                    dgvChuyenKhau.Columns[1].Visible = false;
                    //dgvChuyenKhau.Columns[2].Visible = false;
                    //dgvChuyenKhau.Columns[3].Visible = false;
                    //dgvChuyenKhau.Columns[4].Visible = false;
                    //dgvChuyenKhau.Columns[5].Visible = false;
                    dgvChuyenKhau.Columns[6].Visible = false;
                    dgvChuyenKhau.Columns[7].Visible = false;
                    dgvChuyenKhau.Columns[8].Visible = false;
                    dgvChuyenKhau.Columns[9].Visible = false;
                    dgvChuyenKhau.Columns[10].Visible = false;
                    dgvChuyenKhau.Columns[11].Visible = false;
                    dgvChuyenKhau.Columns[12].Visible = false;
                    dgvChuyenKhau.Columns[13].Visible = false;
                    dgvChuyenKhau.Columns[14].Visible = false;
                    dgvChuyenKhau.Columns[15].Visible = false;
                    //dgvChuyenKhau.Columns[16].Visible = false;
                    //dgvChuyenKhau.Columns[16].Visible = false;
                    btnChiTiet.Enabled = true;
                }
                else
                {
                    dgvChuyenKhau.DataSource = null;
                    txtChuyenTu.ResetText();
                    txtDen.ResetText();
                    txtLyDo.ResetText();
                    dateNgayCap.ResetValue();
                    dateNgayChuyen.ResetValue();
                    btnChiTiet.Enabled = false;
                }

            }

            //tim theo so khai sinh
            if ((txtSoKhaiSinh.Text == "" && rdtKS.Checked == true) || (txtCMND.Text == "" && rdtCMND.Checked == true) || (txtSoSHK.Text == "" && rdtSHK.Checked == true)||(txtHoTenTim.Text==""&& rdtHoTen.Checked==true))
            {
                // 1 and 1  = 1
                MessageBox.Show("Nhập giá trị cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            if (txtSoKhaiSinh.Text != "" && rdtKS.Checked == true)
            {
                // 1 and 0 = 0  rong false
                // 0 and 1 = 0  co   true
                // 0 and 0 = 0  co   false
                KhaiSinhPublic ks = KhaiSinhBLL.LoadKhaiSinhBySoKS(int.Parse(txtSoKhaiSinh.Text));
                if (ks == null)
                {
                    MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtSoKhaiSinh.ResetText();
                    txtSoKhaiSinh.Focus();
                    return;
                }
                NhanKhauPublic nk = NhanKhauBLL.LoadNhanKhauByMaKS(ks.MaKS);
                txtHoTen.Text = nk.HoTen;
                dateNgaySinh.Value = (DateTime)nk.NgaySinh;
                if (nk.GioiTinh == true)
                    sbtGioiTinh.Value = true;
                else
                    sbtGioiTinh.Value = false;
                
                txtTrinhDo.Text = TrinhDoBLL.GetTrinhDoByMaTD(nk.MaTD).TenTrinhDo;
                txtDanToc.Text = DanTocBLL.LoadDanTocByMaDT(nk.MaDT).TenDanToc;
                txtQuocTich.Text = QuocTichBLL.LoadQuocTichByMaQT(nk.MaQT).TenQuocTich;
                txtTonGiao.Text = TonGiaoBLL.LoadTonGiaoByMaTG(nk.MaTG).TenTonGiao;
                txtDiaChi.Text = SoHoKhauBLL.LoadDiaChiSHKByMaNK(nk.MaNK);
                txtCMNDXem.Text = nk.CMND;

                //load khai sinh 
                txtMaKS.Text = ks.MaKS.ToString();
                txtSoKS.Text = ks.SoKhaiSinh.ToString();
                txtHoTenCha.Text = NhanKhauBLL.LoadNhanKhauByMaNK(ks.MaNKC).HoTen;
                txtHoTenMe.Text = NhanKhauBLL.LoadNhanKhauByMaNK(ks.MaNKM).HoTen;
                txtQuanHeChuHo.Text = QuanHeBLL.LoadQuanHeByMaQH(nk.MaQH).TenQuanHe;
                txtQuanHeKS.Text = QuanHeBLL.LoadQuanHeByMaQH(ks.MaQH).TenQuanHe;
                txtCanBoHoTichKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.CanBoHoTich).HoTen;
                txtNguoiDangKiKS.Text = ks.NguoiDangki;
                txtNguoiCapKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.NguoiCap).HoTen;
                dateNgayCapKS.Value = (DateTime)ks.NgayCap;
                cmbCanBoDangKiKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.CanBoDangKi).HoTen;
                cmbNguoiNhanKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.NguoiNhan).HoTen;
                txtChucVuNguoiCapKS.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND(ks.NguoiNhan).MaCV).TenChucVu;
                dateNgayNhapKS.Value = (DateTime)ks.NgayNhap;
                txtNoiCapKS.Text = ThietLapBLL.LoadDiaChiByMaDiaChi(ks.NoiCap);

                //load khai tu
                if (nk.MaKT != 0)
                {
                    cboxTinhTrang.Checked = true;
                    KhaiTuPublic kt = KhaiTuBLL.LoadKhaiTuByMaKT(nk.MaKT);

                    txtSoBaoTu.Text = kt.SoBaoTu.ToString();
                    txtSoChungTu.Text = kt.SoChungTu.ToString();
                    txtNoiCapBaoTu.Text = kt.NoiCapBaoTu;
                    txtNoiChet.Text = kt.NoiChet;
                    txtNoiOCuoiCung.Text = kt.NoiOCuoiCung;
                    txtNoiCapChungTu.Text = ThietLapBLL.LoadDiaChiByMaDiaChi(kt.NoiCap);
                    txtThoiGianChet.Text = kt.NgayChet.ToString();
                    txtNguoiCapKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.NguoiCap).HoTen;
                    txtNguoiKhaiTu.Text = kt.NguoiDangKi;
                    cmbNgayCapBaoTu.Value = kt.NgayCapBaoTu.Value;
                    txtCanBoHoTichKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.CanBoHoTich).HoTen;
                    cmbCanBoDangKiKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.CanBoDangKi).HoTen;
                    txtChucVuKT.Text = ChucVuBLL.LoadChucVuByMaCV(kt.ChucVu).TenChucVu;
                    txtChucVuNguoiNhanKT.Text = ChucVuBLL.LoadChucVuByMaCV(kt.ChucVuNguoiNhan).TenChucVu;
                    dateNgayCapKT.Value = (DateTime)kt.NgayCap;
                    dateNgayNhapKT.Value = (DateTime)kt.NgayNhap;
                    cmbNguoiNhanKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.NguoiNhan).HoTen;
                    txtLyDoChet.Text = kt.LyDoChet;
                    //MessageBox.Show("gege");
                }
                else
                    cboxTinhTrang.Checked = false;
                List<ChuyenKhauPublic> list_ck = ChuyenKhauBLL.LoadChuyenKhauByMaNK(nk.MaNK);
                if (list_ck.Count != 0)
                {
                    dgvChuyenKhau.DataSource = list_ck;
                    //dgvChuyenKhau.Columns[0].HeaderText = "Mã chuyển khẩu";
                    //dgvChuyenKhau.Columns[1].HeaderText = "Mã nhân khẩu";
                    dgvChuyenKhau.Columns[2].HeaderText = "Số";
                    dgvChuyenKhau.Columns[3].HeaderText = "Nơi chuyển";
                    dgvChuyenKhau.Columns[4].HeaderText = "Lý do chuyển";
                    dgvChuyenKhau.Columns[5].HeaderText = "Ngày cấp";
                    //dgvChuyenKhau.Columns[6].HeaderText = "Cán bộ nơi chuyển";
                    dgvChuyenKhau.Columns[7].HeaderText = "Người cấp";
                    //dgvChuyenKhau.Columns[8].HeaderText = "Chức vụ người chuyển";
                    dgvChuyenKhau.Columns[9].HeaderText = "Nơi cấp";
                    //dgvChuyenKhau.Columns[10].HeaderText = "Cán bộ nơi nhận";
                    //dgvChuyenKhau.Columns[11].HeaderText = "Ngày nhận";
                    dgvChuyenKhau.Columns[12].HeaderText = "Người nhận";
                    //dgvChuyenKhau.Columns[13].HeaderText = "Chức vụ người nhận";
                    //dgvChuyenKhau.Columns[14].HeaderText = "Nơi ở trước";
                    dgvChuyenKhau.Columns[15].HeaderText = "Chủ hộ";
                    dgvChuyenKhau.Columns[16].HeaderText = "Quan hệ";

                    dgvChuyenKhau.Columns[0].Visible = false;
                    dgvChuyenKhau.Columns[1].Visible = false;
                    //dgvChuyenKhau.Columns[2].Visible = false;
                    //dgvChuyenKhau.Columns[3].Visible = false;
                    //dgvChuyenKhau.Columns[4].Visible = false;
                    //dgvChuyenKhau.Columns[5].Visible = false;
                    dgvChuyenKhau.Columns[6].Visible = false;
                    dgvChuyenKhau.Columns[7].Visible = false;
                    dgvChuyenKhau.Columns[8].Visible = false;
                    dgvChuyenKhau.Columns[9].Visible = false;
                    dgvChuyenKhau.Columns[10].Visible = false;
                    dgvChuyenKhau.Columns[11].Visible = false;
                    dgvChuyenKhau.Columns[12].Visible = false;
                    dgvChuyenKhau.Columns[13].Visible = false;
                    dgvChuyenKhau.Columns[14].Visible = false;
                    //dgvChuyenKhau.Columns[15].Visible = false;
                    //dgvChuyenKhau.Columns[16].Visible = false;
                    //dgvChuyenKhau.Columns[17].Visible = false;
                    btnChiTiet.Enabled = true;
                }
                else
                {
                    dgvChuyenKhau.DataSource = null;
                    txtChuyenTu.ResetText();
                    txtDen.ResetText();
                    txtLyDo.ResetText();
                    dateNgayCap.ResetValue();
                    dateNgayChuyen.ResetValue();
                    btnChiTiet.Enabled = false;
                }

            }
            if (txtSoSHK.Text != "" && rdtSHK.Checked == true)
            {
                SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(int.Parse(txtSoSHK.Text));
                if (shk == null)
                {
                    MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtSoSHK.ResetText();
                    txtSoSHK.Focus();
                    return;
                }
                List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);
                dgvNhanKhau.DataSource = list_nk;

                //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
                dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
                //dgvNhanKhau.Columns[2].HeaderText = "Mã khai sinh";
                //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
                dgvNhanKhau.Columns[4].HeaderText = "CMND";
                dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
                //dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
                dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
                dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
                dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
                dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
                dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
                //dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
                dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
                dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
                //dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
                dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
                dgvNhanKhau.Columns[0].Visible = false;
                //dgvNhanKhau.Columns[1].Visible = false;
                dgvNhanKhau.Columns[2].Visible = false;
                dgvNhanKhau.Columns[3].Visible = false;
                //dgvNhanKhau.Columns[4].Visible = false;
                //dgvNhanKhau.Columns[5].Visible = false;
                dgvNhanKhau.Columns[6].Visible = false;
                //dgvNhanKhau.Columns[7].Visible = false;
                //dgvNhanKhau.Columns[8].Visible = false;
                dgvNhanKhau.Columns[9].Visible = false;
                dgvNhanKhau.Columns[10].Visible = false;
                dgvNhanKhau.Columns[11].Visible = false;
                dgvNhanKhau.Columns[12].Visible = false;
                dgvNhanKhau.Columns[13].Visible = false;
                dgvNhanKhau.Columns[14].Visible = false;
                dgvNhanKhau.Columns[15].Visible = false;
                //dgvNhanKhau.Columns[16].Visible = false;
            }
            if (txtHoTenTim.Text != "" && rdtHoTen.Checked == true)
            {
                List<NhanKhauPublic> list_nk = NhanKhauBLL.TimKiemTen(txtHoTenTim.Text);
                if (list_nk.Count == 0)
                {
                    MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtHoTenTim.Focus();
                    return;
                }
                else
                {

                    dgvNhanKhau.DataSource = list_nk;

                    //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
                    dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
                    //dgvNhanKhau.Columns[2].HeaderText = "Mã khai sinh";
                    //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
                    dgvNhanKhau.Columns[4].HeaderText = "CMND";
                    dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
                    //dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
                    dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
                    dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
                    dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
                    dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
                    dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
                    //dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
                    dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
                    dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
                    //dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
                    dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
                    dgvNhanKhau.Columns[0].Visible = false;
                    //dgvNhanKhau.Columns[1].Visible = false;
                    dgvNhanKhau.Columns[2].Visible = false;
                    dgvNhanKhau.Columns[3].Visible = false;
                    //dgvNhanKhau.Columns[4].Visible = false;
                    //dgvNhanKhau.Columns[5].Visible = false;
                    dgvNhanKhau.Columns[6].Visible = false;
                    //dgvNhanKhau.Columns[7].Visible = false;
                    //dgvNhanKhau.Columns[8].Visible = false;
                    dgvNhanKhau.Columns[9].Visible = false;
                    dgvNhanKhau.Columns[10].Visible = false;
                    dgvNhanKhau.Columns[11].Visible = false;
                    dgvNhanKhau.Columns[12].Visible = false;
                    dgvNhanKhau.Columns[13].Visible = false;
                    dgvNhanKhau.Columns[14].Visible = false;
                    dgvNhanKhau.Columns[15].Visible = false;
                    //dgvNhanKhau.Columns[16].Visible = false;
                }
            }

        }
        private void dgvNhanKhau_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewCell cell = null;
             
            foreach (DataGridViewCell select in dgvNhanKhau.SelectedCells)
            {
                cell = select;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                txtHoTen.Text = (string)row.Cells[1].Value;
                dateNgaySinh.Value = ((DateTime)row.Cells[5].Value);
                if ((bool)row.Cells[7].Value == true)
                    sbtGioiTinh.Value = true;
                else
                    sbtGioiTinh.Value = false;
                txtDanToc.Text = (DanTocBLL.LoadDanTocByMaDT((byte)row.Cells[9].Value).TenDanToc).ToString();
                txtQuocTich.Text = (QuocTichBLL.LoadQuocTichByMaQT((byte)row.Cells[11].Value).TenQuocTich).ToString();
                txtTonGiao.Text = (TonGiaoBLL.LoadTonGiaoByMaTG((byte)row.Cells[13].Value).TenTonGiao).ToString();
                txtTrinhDo.Text = (TrinhDoBLL.GetTrinhDoByMaTD((byte)row.Cells[14].Value).TenTrinhDo).ToString();
                txtNoiSinh.Text = TinhBLL.LoadTinhByMaT((int)row.Cells[6].Value);
                txtDiaChi.Text = SoHoKhauBLL.LoadDiaChiSHKByMaNK((int)row.Cells[0].Value);
                txtCMNDXem.Text = row.Cells[4].Value.ToString();

                if ((int)row.Cells[12].Value != 0)
                {
                    cboxTinhTrang.Checked = true;
                    KhaiTuPublic kt = KhaiTuBLL.LoadKhaiTuByMaKT((int)row.Cells[12].Value);

                    txtSoBaoTu.Text = kt.SoBaoTu.ToString();
                    txtSoChungTu.Text = kt.SoChungTu.ToString();
                    txtNoiCapBaoTu.Text = kt.NoiCapBaoTu;
                    txtNoiChet.Text = kt.NoiChet;
                    txtNoiOCuoiCung.Text = kt.NoiOCuoiCung;
                    txtNoiCapChungTu.Text = ThietLapBLL.LoadDiaChiByMaDiaChi(kt.NoiCap);
                    txtThoiGianChet.Text = kt.NgayChet.ToString();
                    txtNguoiCapKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.NguoiCap).HoTen;
                    txtNguoiKhaiTu.Text = kt.NguoiDangKi;
                    cmbNgayCapBaoTu.Value = kt.NgayCapBaoTu.Value;
                    txtCanBoHoTichKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.CanBoHoTich).HoTen;
                    cmbCanBoDangKiKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.CanBoDangKi).HoTen;
                    txtChucVuKT.Text = ChucVuBLL.LoadChucVuByMaCV(kt.ChucVu).TenChucVu;
                    txtChucVuNguoiNhanKT.Text = ChucVuBLL.LoadChucVuByMaCV(kt.ChucVuNguoiNhan).TenChucVu;
                    dateNgayCapKT.Value = (DateTime)kt.NgayCap;
                    dateNgayNhapKT.Value = (DateTime)kt.NgayNhap;
                    cmbNguoiNhanKT.Text = NguoiDungBLL.LoadNguoiDungByMaND(kt.NguoiNhan).HoTen;
                    txtLyDoChet.Text = kt.LyDoChet;

                }
                else
                {
                    cboxTinhTrang.Checked = false;

                    txtSoBaoTu.ResetText();
                    txtSoChungTu.ResetText();
                    txtNoiCapBaoTu.ResetText();
                    txtNoiChet.ResetText();
                    txtNoiOCuoiCung.ResetText();
                    txtNoiCapChungTu.ResetText();
                    txtThoiGianChet.ResetText();
                    txtNguoiCapKT.ResetText();
                    txtNguoiKhaiTu.ResetText();
                    cmbNgayCapBaoTu.ResetValue();
                    txtCanBoHoTichKT.ResetText();
                    cmbCanBoDangKiKT.ResetText();
                    txtChucVuKT.ResetText();
                    txtChucVuNguoiNhanKT.ResetText();
                    dateNgayCapKT.ResetText();
                    dateNgayNhapKT.ResetText();
                    cmbNguoiNhanKT.ResetText();
                    txtLyDoChet.ResetText();
                }
                if ((int)row.Cells[2].Value != 0)
                {
                    KhaiSinhPublic ks = KhaiSinhBLL.LoadKhaiSinhByMaKS((int)row.Cells[2].Value);
                    txtMaKS.Text = ks.MaKS.ToString();
                    txtSoKS.Text = ks.SoKhaiSinh.ToString();
                    txtHoTenCha.Text = NhanKhauBLL.LoadNhanKhauByMaNK(ks.MaNKC).HoTen;
                    txtHoTenMe.Text = NhanKhauBLL.LoadNhanKhauByMaNK(ks.MaNKM).HoTen;
                    txtQuanHeChuHo.Text = QuanHeBLL.LoadQuanHeByMaQH((byte)row.Cells[16].Value).TenQuanHe;
                    txtQuanHeKS.Text = QuanHeBLL.LoadQuanHeByMaQH(ks.MaQH).TenQuanHe;
                    txtCanBoHoTichKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.CanBoHoTich).HoTen;
                    txtNguoiDangKiKS.Text = ks.NguoiDangki;
                    txtNguoiCapKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.NguoiCap).HoTen;
                    dateNgayCapKS.Value = (DateTime)ks.NgayCap;
                    cmbCanBoDangKiKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.CanBoDangKi).HoTen;
                    cmbNguoiNhanKS.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.NguoiNhan).HoTen;
                    txtChucVuNguoiCapKS.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND(ks.NguoiNhan).MaCV).TenChucVu;
                    dateNgayNhapKS.Value = (DateTime)ks.NgayNhap;
                    txtNoiCapKS.Text = ThietLapBLL.LoadDiaChiByMaDiaChi(ks.NoiCap);
                }
                else
                {
                    txtMaKS.ResetText();
                    txtSoKS.ResetText();
                    txtHoTenCha.ResetText();
                    txtHoTenMe.ResetText();
                    txtQuanHeChuHo.ResetText();
                    txtQuanHeKS.ResetText();
                    txtCanBoHoTichKS.ResetText();
                    txtNguoiDangKiKS.ResetText();
                    txtNguoiCapKS.ResetText();
                    dateNgayCapKS.ResetValue();
                    cmbCanBoDangKiKS.ResetText();
                    cmbNguoiNhanKS.ResetText();
                    txtChucVuNguoiCapKS.ResetText();
                    dateNgayNhapKS.ResetValue();
                    txtNoiCapKS.ResetText();
                }
                List<ChuyenKhauPublic> list_ck = ChuyenKhauBLL.LoadChuyenKhauByMaNK((int)row.Cells[0].Value);
                if (list_ck.Count != 0)
                {
                    dgvChuyenKhau.DataSource = list_ck;
                    //dgvChuyenKhau.Columns[0].HeaderText = "Mã chuyển khẩu";
                    //dgvChuyenKhau.Columns[1].HeaderText = "Mã nhân khẩu";
                    dgvChuyenKhau.Columns[2].HeaderText = "Số";
                    dgvChuyenKhau.Columns[3].HeaderText = "Nơi chuyển";
                    dgvChuyenKhau.Columns[4].HeaderText = "Lý do chuyển";
                    dgvChuyenKhau.Columns[5].HeaderText = "Ngày cấp";
                    //dgvChuyenKhau.Columns[6].HeaderText = "Cán bộ nơi cấp";
                    dgvChuyenKhau.Columns[7].HeaderText = "Người cấp";
                    //dgvChuyenKhau.Columns[8].HeaderText = "Chức vụ người cấp";
                    dgvChuyenKhau.Columns[9].HeaderText = "Nơi cấp";
                    //dgvChuyenKhau.Columns[10].HeaderText = "Cán bộ nơi nhận";
                    //dgvChuyenKhau.Columns[11].HeaderText = "Ngày nhận";
                    dgvChuyenKhau.Columns[12].HeaderText = "Người nhận";
                    //dgvChuyenKhau.Columns[13].HeaderText = "Chức vụ người nhận";
                    dgvChuyenKhau.Columns[14].HeaderText = "Thường trú";
                    dgvChuyenKhau.Columns[15].HeaderText = "Chủ hộ";
                    dgvChuyenKhau.Columns[16].HeaderText = "Quan hệ";
                    dgvChuyenKhau.Columns[0].Visible = false;
                    dgvChuyenKhau.Columns[1].Visible = false;
                    //dgvChuyenKhau.Columns[2].Visible = false;
                    //dgvChuyenKhau.Columns[3].Visible = false;
                    //dgvChuyenKhau.Columns[4].Visible = false;
                    //dgvChuyenKhau.Columns[5].Visible = false;
                    dgvChuyenKhau.Columns[6].Visible = false;
                    dgvChuyenKhau.Columns[7].Visible = false;
                    dgvChuyenKhau.Columns[8].Visible = false;
                    dgvChuyenKhau.Columns[9].Visible = false;
                    dgvChuyenKhau.Columns[10].Visible = false;
                    dgvChuyenKhau.Columns[11].Visible = false;
                    dgvChuyenKhau.Columns[12].Visible = false;
                    dgvChuyenKhau.Columns[13].Visible = false;
                    dgvChuyenKhau.Columns[14].Visible = false;
                    //dgvChuyenKhau.Columns[15].Visible = false;
                    //dgvChuyenKhau.Columns[16].Visible = false;
                    //dgvChuyenKhau.Columns[16].Visible = false;
                    btnChiTiet.Enabled = true;
                }
                else
                {
                    dgvChuyenKhau.DataSource = null;
                    txtChuyenTu.ResetText();
                    txtDen.ResetText();
                    txtLyDo.ResetText();
                    dateNgayCap.ResetValue();
                    dateNgayChuyen.ResetValue();
                    btnChiTiet.Enabled = false;
                }
            }
        }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {       
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtCMND.Text.Length > 8 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
                btnTim_Click(sender, e);
        }

        private void txtSoKhaiSinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoKhaiSinh.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
                btnTim_Click(sender, e);
        }

        private void txtSoSHK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoSHK.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
                btnTim_Click(sender, e);
        }

        private void rdtCMND_CheckedChanged(object sender, EventArgs e)
        {
            if (rdtCMND.Checked == true)
            {
                txtCMND.Enabled = true;
                txtCMND.Focus();
            }
            else
            {
                txtCMND.Enabled = false;
                txtCMND.ResetText();
            }

        }

        private void rdtKS_CheckedChanged(object sender, EventArgs e)
        {
            if (rdtKS.Checked == true)
            {
                txtSoKhaiSinh.Enabled = true;
                txtSoKhaiSinh.Focus();
            }
            else
            {
                txtSoKhaiSinh.Enabled = false;
                txtSoKhaiSinh.ResetText();
            }
        }

        private void rdtSHK_CheckedChanged(object sender, EventArgs e)
        {

            if (rdtSHK.Checked == true)
            {
                txtSoSHK.Enabled = true;
                txtSoSHK.Focus();
            }
            else
            {
                txtSoSHK.Enabled = false;
                txtSoSHK.ResetText();
            }
        }

        private void UCTimKiem_Load(object sender, EventArgs e)
        {
            txtCMND.Text = "301400274";
            txtSoSHK.Text = "1000002";
            txtSoKS.Text = "1234";
            txtHoTenTim.Text = "Nguyễn Văn a";
            txtCMND.Focus();
        }
        private int _SoChuyenKhau;

        public int SoChuyenKhau
        {
            get { return _SoChuyenKhau; }
            set { _SoChuyenKhau = value; }
        }
        private void dgvChuyenKhau_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewCell cell = null;
             
            foreach (DataGridViewCell select in dgvChuyenKhau.SelectedCells)
            {
                cell = select;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                if(row.Cells[9].Value.ToString().Length>6)
                {
                     txtChuyenTu.Text = row.Cells[9].Value.ToString();
                }
                else
                    txtChuyenTu.Text = ThietLapBLL.LoadDiaChi() ;

                if (row.Cells[3].Value.ToString().Length > 6)
                    txtDen.Text = row.Cells[3].Value.ToString();
                else
                    txtDen.Text = ThietLapBLL.LoadDiaChiByMaDiaChi((int)row.Cells[3].Value);
                dateNgayChuyen.Value = (DateTime)row.Cells[5].Value;
                dateNgayCap.Value = (DateTime)row.Cells[5].Value;
                txtLyDo.Text = row.Cells[4].Value.ToString();
                //txtChuyenTu.Text=row.Cells[].Value;
                _SoChuyenKhau = (int)row.Cells[2].Value;
            }
        }

        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            frmCapChuyenKhau cck = new frmCapChuyenKhau(_SoChuyenKhau);
            cck.ShowDialog();
        }

        private void txtSoSHK_Click(object sender, EventArgs e)
        {
            rdtSHK.Checked = true;
        }

        private void txtSoKhaiSinh_Click(object sender, EventArgs e)
        {
            rdtKS.Checked = true;
        }

        private void txtCMND_Click(object sender, EventArgs e)
        {
            rdtCMND.Checked = true;
        }

        private void rdtHoTen_CheckedChanged(object sender, EventArgs e)
        {
            if (rdtHoTen.Checked == true)
            {
                txtHoTenTim.Enabled = true;
                txtHoTenTim.Focus();
            }
            else
            {
                txtHoTenTim.Enabled = false;
                txtHoTenTim.ResetText();
            }
        }

        
    }
}
